package com.wang.project.email;


import com.wang.project.model.entity.User;
import com.wang.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Component
@Slf4j
public class EmailPayMessageHandle {

    @Autowired(required = false)
    private JavaMailSender emailSender;

    @Resource
    private UserService userService;
    final String myEmailHost = "wang546385234@qq.com";

    /**
     * 支付完成之后 已邮件形式通知对方支付完成
     *
     * @param orderCode 订单号
     */
    public void sendPayMessage(String orderCode, Long userId) {
        User user = userService.getById(userId);
        String userEmail = user.getEmail();
        if (userEmail.isEmpty() || StringUtils.isBlank(userEmail)) {
            log.info("EmailPayMessageHandle sendPayMessage orderCode：{} ，当前用户：{} 还没有绑定邮箱", orderCode, userId);
            return;
        }
        log.info("EmailPayMessageHandle sendPayMessage orderCode:{}，userEmail：{}", orderCode, userEmail);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("openApi支付通知"); // 发送邮件的标题
        message.setText("订单号：" + orderCode + " 已完成支付"); // 发送邮件的内容
        message.setTo(userEmail); // 指定要接收邮件的用户邮箱账号
        message.setFrom(myEmailHost); // 发送邮件的邮箱账号，注意一定要和配置文件中的一致！
        emailSender.send(message); // 调用send方法发送邮件即可
    }
}
